package com.server.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.server.authority.Login;
import com.server.common.base.ProjectInfor;
import com.server.common.base.SessionData;
import com.server.service.ICaseService;
import com.server.service.ICommonDataService;
import com.server.web.DataVector;
import com.server.web.JSONSerielizer;
import com.server.web.OutputUtil;
import com.server.web.RequestInput;
import com.server.web.SessionUtil;

@Controller
@RequestMapping("case")
public class CaseController {
	
	private String tableName = "education.case_warehouse";
	
	@Autowired
	private ICaseService caseService;
	@Autowired
	private ICommonDataService commonDataService;

	/**
	 * 案例列表
	 * @param request
	 * @param response
	 * @return
	 * @author ServerZhang
	 * @date 2016年8月2日
	 */
	@Login
	@RequestMapping("")
	public ModelAndView casePage(HttpServletRequest request,
			HttpServletResponse response) {
		ModelAndView mv = new ModelAndView("case/caseList");
		mv.addObject("rootPath", ProjectInfor.ROOT_PATH);
		return mv;
	}
	@RequestMapping("list")
	public void list(HttpServletRequest request,
			HttpServletResponse response) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String level = SessionUtil.getStringAttri(request, SessionData.levelKey_mng);
		String _whereStr = "valid=1";
		if (StringUtils.isEmpty(level) || !level.equals("1")) {//不是超级管理员
			_whereStr += " and creater='"+userName+"'";
		}
		String sortStr = "createTime desc";
		DataVector<Map<String, Object>> listData = commonDataService.getListData(tableName, _whereStr, sortStr, new RequestInput(request));
		OutputUtil.returnGridData(response, listData.getCount(), JSONSerielizer.toJson(listData.getList(), null, null));
	}
	@RequestMapping("form/{id}")
	public void form(HttpServletRequest request,
			HttpServletResponse response,@PathVariable String id){
		Map<String, Object> formData = commonDataService.getFormData(tableName, "id", id, null, null);
		OutputUtil.returnString(response, JSONSerielizer.toJson(formData, null, null));
	}
	@Login
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,
			HttpServletResponse response) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/case/caseForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "new");
		mv.addObject("state", 0);
		mv.addObject("business", "case");
		mv.addObject("titleName", "新增案例");
		mv.addObject("userName", userName);
		return mv;
	}
	@RequestMapping("save/{id}")
	public void save(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id){
		String operator = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String formData = request.getParameter("formData");
		Map<String, Object> saveMap = JSONSerielizer.toMap(formData);
		
		//处理特殊字符“[]”
		/*if (saveMap != null && saveMap.size() > 0) {
			saveMap.get("");
		}*/
		
		UUID uuid = UUID.randomUUID();
		Boolean isSuccess = true;
		if (id.equals(0)) {
			saveMap.put("uuid", uuid);
			isSuccess = commonDataService.add(operator, tableName, saveMap);
			if (isSuccess) {
				id = caseService.getId(uuid);
			}
		}else {
			isSuccess = commonDataService.update(operator, tableName, "id", id.toString(), saveMap);
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("isSuccess", isSuccess);
		map.put("id", id);
		OutputUtil.returnString(response, JSONSerielizer.toJson(map, null, null));
	}
	@Login
	@RequestMapping("update/{id}/{state}")
	public ModelAndView update(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		
		mv.addObject("pagePath","pages/case/caseForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "edit");
		mv.addObject("state", state);
		mv.addObject("business", "case");
		mv.addObject("titleName", "编辑案例");
		mv.addObject("userName", userName);
		return mv;
	}
	@Login
	@RequestMapping("check/{id}/{state}")
	public ModelAndView check(HttpServletRequest request,
			HttpServletResponse response,@PathVariable Integer id,
			@PathVariable Integer state) {
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		ModelAndView mv = new ModelAndView("/common/editFrame");
		mv.addObject("pagePath","pages/case/caseForm.jsp");
		mv.addObject("rootPath",ProjectInfor.ROOT_PATH);
		mv.addObject("action", "check");
		mv.addObject("state", state);
		mv.addObject("business", "case");
		mv.addObject("titleName", "查看案例");
		mv.addObject("userName", userName);
		return mv;
	}
	 /**
	 * 提交评估
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年8月15日
	 */
	@RequestMapping("toEstimate")
	public void toEstimate(HttpServletRequest request,
			HttpServletResponse response){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String ids = request.getParameter("ids");
		Integer estimate = caseService.toEstimate(ids, userName);
		OutputUtil.returnString(response, estimate.toString());
	}
	 /**
	 * 删除
	 * @param request
	 * @param response
	 * @author ServerZhang
	 * @date 2016年8月15日
	 */
	@RequestMapping("delete")
	public void delete(HttpServletRequest request,
			HttpServletResponse response){
		String userName = SessionUtil.getStringAttri(request, SessionData.nameKey_mng);
		String ids = request.getParameter("ids");
		Integer estimate = caseService.delete(ids, userName);
		OutputUtil.returnString(response, estimate.toString());
	}
}
